<?php

namespace App\Controller\Admin;

use App\Controller\Example\AdminLogExample;
use App\Models\AdminModel;
use App\Models\Conn;
use App\Models\NoticeModel;
use Core\Common\Common;
use Core\Result\Result;

class AdminLog extends NoticeModel
{
    /**
     * 管理后台日志列表
     * entry token,username,startTime,endTime,page,limit
     * return json
     * */
    public function adminLogList($data){
        $assemble = Common::assemble(new AdminLogExample(), $data);
        $identical = Common::identical($data);
        $conn = new Conn();
        $joinSql = " WHERE 1 ";
        if($assemble->username !=null){ $joinSql.=" and `username` ='$assemble->username' ";}
        if($assemble->type !=null){ $joinSql.=" and `type` ='$assemble->type' ";}
        if($assemble->startTime !=null){ $joinSql.=" and UNIX_TIMESTAMP(`time`) >= '$assemble->startTime' ";}
        if($assemble->endTime !=null){ $joinSql.=" and UNIX_TIMESTAMP(`time`) <= '$assemble->endTime' ";}
        $sql = "SELECT `id`, `username`, `type`, `content`,`time` FROM `admin_log` $joinSql order by id desc limit $identical->pageStart,$identical->limit";
        $readConn = $conn->readConn($sql,true);
        $returnData["list"]=$readConn;
        $userCountSql="SELECT count(id) as count FROM `admin_log` $joinSql";
        $returnData["count"] = $conn->readConn($userCountSql)["count"];
        $returnData["total"] = null;
        Result::success($returnData);
    }
    public static function addAdminLog($username,$type,$content){
        $conn = new Conn();
        $adminLogExample = new AdminLogExample();
        $adminLogExample->username = $username;
        $adminLogExample->type = $type;
        $adminLogExample->content = $content;
        AdminModel::addAdminLog($conn,$adminLogExample);
    }
}